<?php
/**
 * User: krisbuist
 * Date: 1/31/12
 * Time: 3:49 PM
 */
class PostGenerator extends Controller
{
    private $step = 1000;

    private $minTagsPerPost = 0;
    private $maxTagsPerPost = 10;

    private $minWordsPerPost = 50;
    private $maxWordsPerPost = 500;

    private $minWordsPerTitle = 2;
    private $maxWordsPerTitle = 10;

    private $numberOfAuthors;

    private $categories;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('/generators/RandomTextGenerator');
        $this->numberOfAuthors = $this->em->createQuery("SELECT COUNT(u) FROM User u")->getSingleScalarResult();
        $this->categories = $this->em->getRepository('Category')->findAll();
        //$this->randomtextgenerator = new RandomTextGenerator();
    }


    public function generatePosts()
    {
        try
        {
            for ($i = 0; $i < $this->step; $i++)
            {
                $post = $this->generateRandomPost();
                $this->em->persist($post);
            }

            $this->em->flush();
            //sleep(1);
        }
        catch (PDOException $e)
        {
            echo $e;
        }


        $this->forwardBrowserIfNotFinished();
    }

    public function forwardBrowserIfNotFinished()
    {
        $numberOfPosts = $this->em->createQuery("SELECT COUNT(p) FROM Post p")->getSingleScalarResult();
        if ($numberOfPosts >= 10000) {
            echo 'Finished inserting posts!';
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=/generators/postGenerator/generatePosts">';
            echo '<title>' . $numberOfPosts . '</title>';
        }
    }

    public function generateRandomPost()
    {
        $post = new Post();

        $post->setAuthor($this->getRandomAuthor());
        $post->setCategory($this->getRandomCategory());
        $post->setContent($this->getRandomPostContent());
        $post->setTitle($this->getRandomTitle());

        $randomTags = $this->getRandomTags();

        foreach ($randomTags as $tag)
        {
            $post->addTag($tag);
        }

        return $post;
    }

    /**
     * @return string
     */
    private function getRandomTitle()
    {
        return $this->randomtextgenerator->getContent(rand($this->minWordsPerTitle, $this->maxWordsPerTitle), 'plain', false);
    }

    /**
     * @return Post_Content
     */
    public function getRandomPostContent()
    {
        $content = new Post_Content();
        $content->setContent($this->randomtextgenerator->getContent(rand($this->minWordsPerPost, $this->maxWordsPerPost), 'html', false));
        return $content;
    }

    /**
     * @return Category
     */
    private function getRandomCategory()
    {
        return $this->categories[rand(0, sizeof($this->categories) - 1)];
    }

    /**
     * @return User
     */
    private function getRandomAuthor()
    {
        $author = null;
        while ($author == null)
        {
            $author = $this->em->find('User', rand(1, $this->numberOfAuthors));
        }
        return $author;
    }

    /**
     * @return array
     */
    private function getRandomTags()
    {
        $tags = array();
        $numberOfTags = rand($this->minTagsPerPost, $this->maxTagsPerPost);

        while (sizeof($tags) < $numberOfTags)
        {
            $newTag = $this->getRandomTag();
            $alreadyInResult = false;
            foreach ($tags as $tag)
            {
                if ($tag->getName() == $newTag) {
                    $alreadyInResult = true;
                }
            }
            if (!$alreadyInResult) {
                $tags[] = $newTag;
            }
        }

        return $tags;
    }

    /**
     * @return Tag
     */
    public function getRandomTag()
    {
        $tagName = $this->randomtextgenerator->getRandomWord();

        $tagQuery = $this->em->createQuery('SELECT t FROM Tag t WHERE t.name = :name');
        $tagQuery->setParameter('name', $tagName);
        $tag = $tagQuery->getOneOrNullResult();

        if ($tag === null) {
            $tag = new Tag();
            $tag->setName($tagName);
        }
        return $tag;
    }
}
